#include<disp_manager.h>


static int fd_fb;
static struct fb_var_screeninfo var;	/* Current var */
static int screen_size;
static unsigned char *fb_base;

int fbDeviceInit(void)
{
   fd_fb = open("/dev/fb0", O_RDWR);
	if (fd_fb < 0)
	{
		printf("can't open /dev/fb0\n");
		return -1;
	}
	if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
	{
		printf("can't get var\n");
		return -1;
	}

	screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
	fb_base = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
	if (fb_base == (unsigned char *)-1)
	{
		printf("can't mmap\n");
		return -1;
	}

	return 0;	
}

int fbDeviceExit(void)
{
	munmap(fb_base , screen_size);
	close(fd_fb);
	return 0;
}

int fbGetBuffer(PDispBuffer ptDispBuffer)
{
	ptDispBuffer->iXres = var.xres;
	ptDispBuffer->iYres = var.yres;
	ptDispBuffer->iBpp  = var.bits_per_pixel;
	ptDispBuffer->buffer = (char*)fb_base;
	return 0;
}

int fbFlushRegion(PRegion ptRegion, PDispBuffer ptDispBuff)
{
	return 0;
}

static DispOpr g_tFramebufferOpr = {
	.name = "fb",
	.DeviceInit = fbDeviceInit,
	.DeviceExit = fbDeviceExit,
	.GetBuffer  = fbGetBuffer,
	.FlushRegion = fbFlushRegion,
};

void FrameBufferRegister(void)
{
	RegisterDisplay(&g_tFramebufferOpr);
}

